package com.newway.wechat.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newway.common.Result;
import com.newway.util.HttpUtil;
import com.newway.wechat.entity.CaiPuEntity;
import com.newway.wechat.mapper.CaiPuMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/wechat/cook")
public class CaiPuController {

    @Resource
    CaiPuMapper caiPuMapper;

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @GetMapping("/query")
    public Result query(String name){
        if(StringUtils.isBlank(name)){
            return Result.fail("请输入查询条件");
        }
        String countKey = "CountKey_"+ LocalDate.now();
        String counts = stringRedisTemplate.opsForValue().get(countKey);
        if(StringUtils.isBlank(counts)){
            stringRedisTemplate.opsForValue().set(countKey,"1");
        }
        counts = stringRedisTemplate.opsForValue().get(countKey);
        QueryWrapper<CaiPuEntity> caiPuEntityQueryWrapper = new QueryWrapper<>();
        caiPuEntityQueryWrapper.like("cp_name",name);
        List<CaiPuEntity> caiPuEntities = caiPuMapper.selectList(caiPuEntityQueryWrapper);
        if(Integer.parseInt(counts)<=49 && caiPuEntities.isEmpty()){
            String forObject = HttpUtil.getForObject("http://apis.juhe.cn/fapigx/caipu/query?key=a09925ca852ac7550750f2fcf64693fc&page=1&num=100&word=" + name, String.class);
            JSONObject jsonObject = JSONObject.parseObject(forObject);
            if("0".equals(jsonObject.getString("error_code"))){
                JSONObject result = jsonObject.getJSONObject("result");
                JSONArray list = result.getJSONArray("list");
                stringRedisTemplate.opsForValue().set(countKey,(Integer.parseInt(counts)+1)+"");
                for(int i=0;i<list.size();i++){
                    JSONObject jsonObject1 = list.getJSONObject(i);
                    CaiPuEntity caiPuEntity = new CaiPuEntity();
                    caiPuEntity.setTexing(jsonObject1.getString("texing"));
                    caiPuEntity.setCpName(jsonObject1.getString("cp_name"));
                    caiPuEntity.setTiaoliao(jsonObject1.getString("tiaoliao"));
                    caiPuEntity.setTishi(jsonObject1.getString("tishi"));
                    caiPuEntity.setTypeId(jsonObject1.getInteger("type_id"));
                    caiPuEntity.setYuanliao(jsonObject1.getString("yuanliao"));
                    caiPuEntity.setZuofa(jsonObject1.getString("zuofa"));
                    caiPuEntity.setTypeName(jsonObject1.getString("type_name"));
                    caiPuMapper.insert(caiPuEntity);
                    caiPuEntities.add(caiPuEntity);
                }
            }
        }
        return Result.success(caiPuEntities);
    }
}
